let tableHeight = $('body').height() - 210;
//传值(型号名称)
let Name = localStorage.getItem('name');

//获取tab 类别
function getType() {
	var typeList = [];
	$.ajax({
		type: 'get',
		url: http + 'zltype',
		data: 'project_name=' + Name,
		dataType: "json",
		async: true,
		success: function(result) {
			console.log(result, '获取tab 类别')
			if (result.code == 200) {
				var typeList = result.data;
				var typeHtml = '';
				for (let i = 0; i < typeList.length; i++) {
					typeHtml += `
					<button class="butt" onclick="tab_click(this)" xh="${typeList[i].xh}">${typeList[i].lb}</button>`
				}
				$(".messagetype").html(typeHtml);

				var arr = document.getElementsByTagName('button');
				for (var i = 0; i < arr.length; i++) {
					arr[i].getAttribute('xh') == 0 ?  arr[i].className = "butted" : arr[i].className = "butt";
					arr[i].onclick = function() {
						var xh = this.getAttribute('xh');
						getTableData(xh);
						//this是当前激活的按钮，在这里可以写对应的操作    
						for (var j = 0; j < arr.length; j++) {
							if (this == arr[j]) {
								this.className = "butted";
							} else {
								arr[j].className = "butt";
							}
						}
					}
				}

			} else {
				console.log(result);
			}
		},
		error: function() {
			console.log('访问失败');
		}
	});
}
//默认tab 页面为0
let xh = 0
//切换tab 更新页面
function tab_click(item) {
	var xh = item.getAttribute('xh');
	console.log(xh)
	getTableData(xh)
}

//表格内容
var userData = 2;
var unitId = 1;
var searchVal = '';
var page = 1;
let limit = 10;
let coloms = [
	[{
			field: 'ZL_QUESTION',
			title: '成本项目',
			width: '240',
			templet: function(d) {
				return `<div class="name">
	 					<p>${d.ZL_QUESTION}</p>
	 				</div>`
			}
		},
		{
			field: 'CALCULATION_METHOD',
			title: '计算方法',
			align: 'right',
			width: '246'
		},
		{
			field: 'JOB_DESCRIPTION',
			title: '工作描述',
			width: '170',
			align: 'right',
		},
		{
			field: 'CALCULATION_BASE',
			title: '计算依据',
			width: '170',
			align: 'right'
		},
		{
			field: 'REMARKS',
			title: '备注',
			width: '119',
			align: 'right',
		}, {
			field: 'LOSS_MONEY',
			title: '损失金额',
			width: '119',
			sort: true,
			align: 'right',
			templet: function(d) {
				return `${pricefmt(d.LOSS_MONEY)}`
			}
		},
		{
			title: '操作',
			width: '119',
			align: 'center',
			fixed: "right",
			templet: function(d) {
				return `<a href="javascript:;" style="color:#165DFF">查看说明</a>`
			}
		}
	]
];

function getTableData(xh) {
	console.log(xh);
	var data = [];
	$.ajax({
		type: 'get',
		url: http + 'zlmx',
		data: 'project_name=' + Name + '&lb=' + xh,
		dataType: "json",
		async: true,
		success: function(result) {
			console.log(result, '表格内容')
			if (result.code == 200) {
				var data = result.data;
				tableDrawing(data) // 数据传到 table组件
				// pagesDrawing(result) // 数据传到 分页组件
			} else {
				console.log(result);
			}
		},
		error: function() {
			console.log('访问失败');
		}
	});
}






function tableDrawing(data) {
	layui.use('table', function() {
		let table = layui.table;
		table.render({
			elem: '#tableList',
			cols: coloms,
			data: data, // 数据
			limit: limit, // 每页显示的条数
			height: tableHeight,
		});
	})
}

// function pagesDrawing(data) {
// 	layui.use('laypage', function() {
// 		let laypage = layui.laypage;
// 		laypage.render({
// 			elem: 'laypage', //注意，这里的 page 是 ID，不用加 # 号
// 			count: data.record_total, //数据总数，从服务端得到
// 			theme: '#E8F3FF',
// 			limit: limit, // 每页条数
// 			prev: '<em><</em>',
// 			next: '<em>></em>',
// 			layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
// 			jump: function(obj, first) {
// 				//首次不执行
// 				if (!first) {
// 					limit = obj.limit;
// 					numpage(obj.curr, obj.limit) // 分页点击传参 
// 				}
// 			}
// 		});
// 	})

// };

// function numpage(curr, page_count) { // 分页切换-数据请求
// 	var list_parameter = {
// 		page: curr,
// 		page_size: page_count,
// 		userid: userData,
// 		search_str: searchVal,
// 	};
// 	$.ajax({
// 		url: http + 'reportwork/myJoinedProject.action',
// 		data: list_parameter,
// 		dataType: "json",
// 		async: true,
// 		success: function(result) {
// 			console.log(result);
// 			if (result.code == 200) {
// 				var data = result.data;
// 				tableDrawing(data) // 数据传到 table组件
// 			} else {
// 				console.log(result);
// 			}
// 		},
// 		error: function() {
// 			console.log('访问失败');
// 		}
// 	});
// };


$(function() {
	getTableData(xh);
	getType()


	let isFocus = false;
	$('.wrapper_search .search_box input').focus(function() {
		isFocus = true
	})
	$('.wrapper_search .search_box input').blur(function() {
		isFocus = false
	})
	$(document).keypress(function(event) {
		let key = event.keyCode;
		searchVal = $('.wrapper_search .search_box input').val()
		if (key == '13' && isFocus) {
			getTableData()
		}
	})
	$('.wrapper_search .search_box img').click(function() {
		searchVal = $('.wrapper_search .search_box input').val()
		getTableData()
	})

})
